package com.creator.pangu.common.utils;

import cn.hutool.core.date.DateTime;
import com.creator.pangu.common.enums.HTTPStatusCode;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.sql.Timestamp;
import java.time.LocalDateTime;

/**
 *
 * @param <T>
 */
@Data
public class R<T> implements Serializable{
    @Serial
    private static final long serialVersionUID = -8617492757256657566L;

    private boolean isSuccess;

    private Integer code;

    private String msg;

    private T data;

    private DateTime timestamp;

    public R() {
        this.isSuccess = true;
        this.code = HTTPStatusCode.SUCCESS.getCode();
        this.msg = HTTPStatusCode.SUCCESS.getMsg();
        this.timestamp = DateTime.now();
    }

    public R(boolean success) {
        this.isSuccess = success;
        this.code = success ? HTTPStatusCode.SUCCESS.getCode() : HTTPStatusCode.ERROR.getCode();
        this.msg = success ? HTTPStatusCode.SUCCESS.getMsg() : HTTPStatusCode.ERROR.getMsg();
        this.timestamp = DateTime.now();
    }

    /**
     *  操作成功，无实际data返回
     * @return
     * @param <T>
     */
    public static <T> R<T> success() {
        R<T> r = new R<>();
        r.setSuccess(true);
        r.setCode(HTTPStatusCode.SUCCESS.getCode());
        r.setMsg(HTTPStatusCode.SUCCESS.getMsg());
        return r;
    }

    /**
     * 操作成功，返回data
     * @param data
     * @return
     * @param <T>
     */
    public static <T> R<T> success(T data) {
        R<T> r = new R<>();
        r.setSuccess(true);
        r.setCode(HTTPStatusCode.SUCCESS.getCode());
        r.setMsg(HTTPStatusCode.SUCCESS.getMsg());
        r.setData(data);
        return r;
    }

    /**
     * 根据状态码，获取状态码预置的错误信息
     * @param httpStatusCode
     * @return
     * @param <T>
     */
    public static <T> R<T> error(HTTPStatusCode httpStatusCode) {
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setCode(httpStatusCode.getCode());
        r.setMsg(httpStatusCode.getMsg());
        return r;
    }

    /**
     *  自定义错误描述返回
     * @param httpStatusCode
     * @param msg 自定义错误描述
     * @return
     * @param <T>
     */
    public static <T> R<T> error(HTTPStatusCode httpStatusCode, String msg) {
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setCode(httpStatusCode.getCode());
        r.setMsg(msg);
        return r;
    }

    /**
     *  错误返回，带data
     * @param httpStatusCode
     * @param data
     * @return
     * @param <T>
     */
    public static <T> R<T> error(HTTPStatusCode httpStatusCode, T data) {
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setCode(httpStatusCode.getCode());
        r.setMsg(httpStatusCode.getMsg());
        r.setData(data);
        return r;
    }

    /**
     *  全自定义异常返回
     * @param httpStatusCode
     * @param msg
     * @param data
     * @return
     * @param <T>
     */
    public static <T> R<T> error(HTTPStatusCode httpStatusCode, String msg ,T data) {
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setCode(httpStatusCode.getCode());
        r.setMsg(msg);
        r.setData(data);
        return r;
    }
}
